package com.jinsilu.freight.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jinsilu.freight.common.MyVBActivity;
import com.jinsilu.freight.databinding.ActivityUploadPolicyOfInsuranceBinding;
import com.jinsilu.freight.helper.ShowImageHelper;
import com.jinsilu.freight.http.model.HttpData;
import com.jinsilu.freight.http.request.GetInsuranceApi;
import com.jinsilu.freight.http.request.UpdateImageApi;
import com.jinsilu.freight.http.request.VerifyInsuranceApi;
import com.jinsilu.freight.http.response.FileBean;
import com.jinsilu.freight.http.response.InsuranceBean;
import com.jinsilu.freight.http.response.DictBean;

import java.io.File;

/**
 * 上传保险单照片
 *
 * @author yangyang
 * @time 2020-11-11 17:35
 */
public class UploadPolicyOfInsuranceActivity extends MyVBActivity<ActivityUploadPolicyOfInsuranceBinding> {

    private String picUrl;

    private DictBean dictBean;

    @Override
    protected void initView() {
        binding.tvNotificationTips.setVisibility(View.VISIBLE);
        binding.tvNotificationTips.setText("拍摄图片时，请保持画面文字的清晰。");

        setOnClickListener(binding.ivPolicyOfInsurancePhoto, binding.tvPolicyOfInsuranceName, binding.tvBtnSubmitCertificate);

    }

    @Override
    protected void initData() {
        getInsuranceStatus();
    }

    /**
     * 获取保险单状态
     */
    private void getInsuranceStatus() {
        EasyHttp.get(this)
                .api(new GetInsuranceApi())
                .request(new HttpCallback<HttpData<InsuranceBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<InsuranceBean> data) {
                        InsuranceBean insuranceBean = data.getData();
                        setCertificateStatus(insuranceBean);
                    }
                });
    }

    private void setCertificateStatus(InsuranceBean insuranceBean) {
        switch (insuranceBean.getInsuranceCertificationStatus()) {
            case -1: // 未认证
            case 0: // 认证失败
            case 1: // 认证成功
            case 2: // 提交全部认证资料 认证中
                picUrl = insuranceBean.getInsuranceCertificate();
                ShowImageHelper.showNormal(this, insuranceBean.getInsuranceCertificate(), binding.ivPolicyOfInsurancePhoto);
                binding.etPolicyNumber.setText(insuranceBean.getInsuranceCertificateNumber());
                binding.tvPolicyOfInsuranceName.setText(insuranceBean.getInsuranceCompany());
                binding.tvPolicyOfInsuranceName.setEnabled(true);
                binding.tvBtnSubmitCertificate.setEnabled(true);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        if (v == binding.ivPolicyOfInsurancePhoto) {
            startImageSelectActivity(0);
        } else if (v == binding.tvPolicyOfInsuranceName) {
            startActivityForResult(InsuranceCompanyListActivity.class, new OnActivityCallback() {
                @Override
                public void onActivityResult(int resultCode, @Nullable Intent data) {
                    if (RESULT_OK == resultCode && null != data) {
                        dictBean = (DictBean) data.getSerializableExtra("insurance");
                        binding.tvPolicyOfInsuranceName.setText(null == dictBean ? "" : dictBean.getDictLabel());
                    }
                }
            });
        } else if (v == binding.tvBtnSubmitCertificate) {
            submitInfo();
        }
    }

    /**
     * 车辆保险认证
     */
    private void submitInfo() {
        if (TextUtils.isEmpty(picUrl)) {
            toast("请上传保险单照片");
            return;
        }
        String insuranceNumber = binding.etPolicyNumber.getText().toString().trim();
        if (TextUtils.isEmpty(insuranceNumber)) {
            toast("请输入请输入保险单号");
            return;
        }
        String insuranceName = binding.tvPolicyOfInsuranceName.getText().toString().trim();
        if (TextUtils.isEmpty(insuranceName)) {
            toast("请选择保险公司名称");
            return;
        }
        EasyHttp.post(this)
                .api(new VerifyInsuranceApi()
                        .setInsuranceCertificateUrl(picUrl)
                        .setInsuranceCertificateNumber(insuranceNumber)
                        .setInsuranceCompany(dictBean.getDictLabel())
                        .setInsuranceCompanyCode(dictBean.getDictValue()))
                .request(new HttpCallback<HttpData<Void>>(this) {

                    @Override
                    public void onSucceed(HttpData<Void> data) {
                        setResult(RESULT_OK);
                        finish();
                    }
                });
    }

    /**
     * 启动图片选择
     */
    private void startImageSelectActivity(int index) {
        ImageSelectVBActivity.start(this, new ImageSelectVBActivity.OnPhotoSelectListener() {

            @Override
            public void onSelected(File file) {
//                File file = CommonHelper.getFileFromUri(data);
                uploadImage(file);
            }

            @Override
            public void onCancel() {
            }
        });
    }

    private void uploadImage(File file) {
        // 上传文件
        EasyHttp.post(this)
                .api(new UpdateImageApi()
                        .setImage(file))
                .request(new HttpCallback<HttpData<FileBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<FileBean> data) {
//                        FileUtils.delete(file);
                        String imgUrl = data.getData().getUrl();
                        picUrl = imgUrl;
                        ShowImageHelper.showNormal(UploadPolicyOfInsuranceActivity.this, imgUrl, binding.ivPolicyOfInsurancePhoto);
                    }
                });
    }
}
